package com.siblings.loan.ui.steps.view

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import com.lib.base.utils.ImageUtil
import com.siblings.lib_permission.OnPermissionCallback
import com.siblings.lib_permission.Permission
import com.siblings.lib_permission.XXPermissions
import com.siblings.loan.R
import com.siblings.loan.app.Apps
import com.siblings.loan.databinding.ItemStepSelfieBinding
import com.siblings.loan.http.ApiService
import com.siblings.loan.http.ApiService.addCommonParams
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.ui.steps.act.SibCameraSelfieAct
import com.siblings.loan.ui.steps.act.SibStepsAct
import com.siblings.loan.utils.event.SibEventType
import org.json.JSONObject
import java.io.File

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/12 15:13
 * @Annotation：步骤自拍照样式
 */
@SuppressLint("NotifyDataSetChanged")
class SibStepSelfieView(private val stepAct: SibStepsAct) {
    var binding = ItemStepSelfieBinding.inflate(stepAct.layoutInflater)

    //本地预览地址
    private var faceImgPath = ""
    //拍摄的头像地址
    private var faceImgUrl = ""
    init {
        //初始化Views
        initSibSelfieViews()
        //初始化数据
        initSibSelfieData()
        //初始化事件监听
        initSibSelfieListener()
    }

    /**
     * Selfie样式
     */
    fun includeSibStepSelfieView(): View {
        return binding.root
    }

    /**
     * 初始化View
     */
    private fun initSibSelfieViews(){
        showSibSelfieBeforeView()
    }

    /**
     * 初始化数据
     */
    private fun initSibSelfieData(){
    }

    /**
     * 初始化事件
     */
    private fun initSibSelfieListener(){
        binding.imgView.setOnClickListener {
            if (faceImgPath.isEmpty()){
                binding.btnNext.callOnClick()
            }
        }
        //删除照片
        binding.imgRemove.setOnClickListener {
            if (faceImgPath.isNotEmpty()){
                HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_STEP_SELFIE_REMOVE_PHOTO,faceImgUrl)
                //删除图片
                val imgFile = File(faceImgPath)
                if (imgFile.isFile){
                    imgFile.delete()
                }
            }
            faceImgPath = ""
            faceImgUrl = ""
            showSibSelfieBeforeView()
        }
        //提交
        binding.btnNext.setOnClickListener {
            //开始人脸拍照
            if (faceImgUrl.isEmpty()){
                if (XXPermissions.isGranted(stepAct, Permission.CAMERA)){
                    openSibSelfieCamera()
                } else {
                    XXPermissions.with(stepAct)
                        // 申请多个权限
                        .permission(Permission.CAMERA)
                        .request(object: OnPermissionCallback {
                            override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                                HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_PERMISSION_SYSTEM_AGREE,Permission.CAMERA)
                                openSibSelfieCamera()
                            }
                            override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                                HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_PERMISSION_SYSTEM_DENY,Permission.CAMERA)
                                XXPermissions.startPermissionActivity(stepAct,permissions)
                            }
                        })
                }
                return@setOnClickListener
            }
            //提交
            val params = JSONObject().addCommonParams().apply {
                put("path", ApiService.API_PATH.SIB_API_OCR_INFO_PATH)
                put("cardType","FACE")//证件类型（PASSPORT、IDCARD、DRIVING、FACE）
                put("faceImgUrl",faceImgUrl)//人脸图片
            }
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_STEP_SELFIE_CONFIRM_SUBMIT,"")
            stepAct.mvpPresenter.onSibSubmitProcessAction(params)
        }
    }

    /**
     * 显示未拍照View
     */
    private fun showSibSelfieBeforeView(){
        binding.imgView.setImageResource(R.mipmap.pic_step_selfie_default_avater)
        binding.imgRemove.visibility = View.INVISIBLE
        binding.btnNext.text = Apps.app.getString(R.string.step_selfie_take)
    }

    /**
     * 显示拍照之后的View
     */
    fun showSibSelfieAfterView(imgPath: String,imgUrl: String){
        faceImgPath = imgPath
        faceImgUrl = imgUrl
        ImageUtil.instance!!.loadImageUrl(stepAct,binding.imgView,faceImgPath)
        binding.imgRemove.visibility = View.VISIBLE
        binding.btnNext.text = Apps.app.getString(R.string.step_selfie_submit)
    }

    /**
     * 打开相机
     */
    private fun openSibSelfieCamera(){
        HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_STEP_SELFIE_START_TAKE,"")
        stepAct.faceCameraResultLauncher.launch(Intent(stepAct, SibCameraSelfieAct::class.java))
    }

    fun onDestroy() {
        faceImgPath = ""
        faceImgUrl = ""
    }
}